package dm.ast;

import test.envset.GlobalState;

/**
 * select语句中的having子句
 */
public class DmHaving extends DmASTNode{
    public DmBoolExpression havingCondition = null;

    @Override
    public void fill(GlobalState globalState) {
        if(!isCompleted){
            havingCondition.fill(globalState);
            isCompleted = true;
        }
    }

    @Override
    public void setWidt() {
        if(havingCondition != null){
            havingCondition.setWidt();
            wid_t = 1 + havingCondition.wid_t;
        }else {
            wid_t = 1;
        }
    }

    @Override
    public void setDept() {
        if(havingCondition != null){
            havingCondition.setDept();
            dep_t = havingCondition.dep_t;
        }else {
            dep_t = 1;
        }
    }


    @Override
    public DmDataType getExpressionType() {
        return null;
    }

    @Override
    public String toSql(){
        if(havingCondition == null){
            return " " + this.getClass().getSimpleName();
        }else {
            return String.format(" HAVING %s" , havingCondition.toSql());
        }
    }

    //处理sql错误
    //-4097 | 单行子查询返回多行
    public void fixErr4097(){
        if(havingCondition != null){
            havingCondition.fixErr4097();
        }
    }
}
